package com.javaee.weixin.xiaochengxu.dto;

import java.io.Serializable;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import com.javaee.weixin.xiaochengxu.tools.MD5Util;

/**
 * 
 * 统一下单回调接口结果<br>
 * 
 * @author songzx
 * @version 1.0,2017年4月22日 下午5:44:49
 * @since wxapi 0.0.1
 */
@JsonInclude(value = Include.NON_EMPTY)
@JacksonXmlRootElement(localName = "xml")
public class UnifiedorderResultDto implements Serializable {
	public static final String TYPE_SUCCESS = "SUCCESS";
	public static final String TYPE_FAIL = "FAIL";

	@JacksonXmlProperty(localName = "return_code")
	String returnCode;
	@JacksonXmlProperty(localName = "return_msg")
	String returnMsg;

	String appid;
	@JacksonXmlProperty(localName = "mch_id")
	String mchId;
	@JacksonXmlProperty(localName = "device_info")
	String deviceInfo;
	@JacksonXmlProperty(localName = "nonce_str")
	String nonceStr;

	@JacksonXmlProperty(localName = "result_code")
	String resultCode;
	@JacksonXmlProperty(localName = "err_code")
	String errCode;
	@JacksonXmlProperty(localName = "err_code_des")
	String errCodeDes;

	@JacksonXmlProperty(localName = "trade_type")
	String tradeType;
	@JacksonXmlProperty(localName = "prepay_id")
	String prepayId;
	@JacksonXmlProperty(localName = "code_url")
	String codeUrl;
	String sign;

	public boolean checkSign(String key) {
		StringBuilder sb = new StringBuilder();
		if (StringUtils.isNotBlank(this.appid)) {
			sb.append("appid=" + this.appid);
		}
		if (StringUtils.isNotBlank(this.codeUrl)) {
			sb.append("&code_url=" + this.codeUrl);
		}
		if (StringUtils.isNotBlank(this.deviceInfo)) {
			sb.append("&device_info=" + this.deviceInfo);
		}
		if (StringUtils.isNotBlank(this.errCode)) {
			sb.append("&err_code=" + this.errCode);
		}
		if (StringUtils.isNotBlank(this.errCodeDes)) {
			sb.append("&err_code_des=" + this.errCodeDes);
		}
		if (StringUtils.isNotBlank(this.mchId)) {
			sb.append("&mch_id=" + this.mchId);
		}
		if (StringUtils.isNotBlank(this.nonceStr)) {
			sb.append("&nonce_str=" + this.nonceStr);
		}
		if (StringUtils.isNotBlank(this.prepayId)) {
			sb.append("&prepay_id=" + this.prepayId);
		}
		if (StringUtils.isNotBlank(this.resultCode)) {
			sb.append("&result_code=" + this.resultCode);
		}
		if (StringUtils.isNotBlank(this.returnCode)) {
			sb.append("&return_code=" + this.returnCode);
		}
		if (StringUtils.isNotBlank(this.returnMsg)) {
			sb.append("&return_msg=" + this.returnMsg);
		}
		if (StringUtils.isNotBlank(this.tradeType)) {
			sb.append("&trade_type=" + this.tradeType);
		}
		sb.append("&key=" + key);

		String tmpSign = MD5Util.getInstance().compile(sb.toString()).toUpperCase();
		if (tmpSign.equals(this.sign)) {
			return true;
		}
		return false;
	}

	public String getReturnCode() {
		return returnCode;
	}

	public void setReturnCode(String returnCode) {
		this.returnCode = returnCode;
	}

	public String getReturnMsg() {
		return returnMsg;
	}

	public void setReturnMsg(String returnMsg) {
		this.returnMsg = returnMsg;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getMchId() {
		return mchId;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	public String getDeviceInfo() {
		return deviceInfo;
	}

	public void setDeviceInfo(String deviceInfo) {
		this.deviceInfo = deviceInfo;
	}

	public String getNonceStr() {
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}

	public String getResultCode() {
		return resultCode;
	}

	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	public String getErrCode() {
		return errCode;
	}

	public void setErrCode(String errCode) {
		this.errCode = errCode;
	}

	public String getErrCodeDes() {
		return errCodeDes;
	}

	public void setErrCodeDes(String errCodeDes) {
		this.errCodeDes = errCodeDes;
	}

	public String getTradeType() {
		return tradeType;
	}

	public void setTradeType(String tradeType) {
		this.tradeType = tradeType;
	}

	public String getPrepayId() {
		return prepayId;
	}

	public void setPrepayId(String prepayId) {
		this.prepayId = prepayId;
	}

	public String getCodeUrl() {
		return codeUrl;
	}

	public void setCodeUrl(String codeUrl) {
		this.codeUrl = codeUrl;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}
	
}
